// pages/street/add/add.js
var streetservice = require("../../../apiservice/street/streetservice");
var userinfo = wx.getStorageSync('useridentity');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		region: ['湖南省', '长沙市', '天心区'],
		streetName: '',
		userinfo: [],
    showDistrict:false
	},

  districtTap:function(e){
    this.setData({ showDistrict:true});
  },
	/**
	 * 当地区切换时，获取对应地区的街道信息
	 * @param {*} e 
	 */
	bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.currentTarget.dataset);
    this.setData({
      showDistrict: false
    });
    var provinceName = e.detail.currentTarget.dataset.province;
    var cityName = e.detail.currentTarget.dataset.city;
    var areaName = e.detail.currentTarget.dataset.area;
    var region = [];
    region.push(provinceName);
    region.push(cityName);
    region.push(areaName);
		this.setData({
      region: region
		})
	},

	onSave: function(e) {
		var that = this;
		var province = this.data.region[0];
		var city = this.data.region[1];
		var area = this.data.region[2];
		var type = this.data.type;
    if(this.data.streetName==''){
      wx.showToast({
        title: '街道名称不能为空',
        icon: 'none',
        duration: 1500
      })

      return;
    }
		var streetObj = {
			new_name: this.data.streetName,
			provinceName: province,
			cityName: city,
			areaName: area
		};
		streetservice.addStreetInfo(streetObj).then(function(success) {
			console.log(success);
			wx.showToast({
				title: '添加成功',
				icon: 'success',
				duration: 3000
			})
			let pages = getCurrentPages();
			let prevPage = pages[pages.length - 2];
			if (province && city && area &&
				prevPage.data.hasOwnProperty("region")) {
				prevPage.setData({
					region: [province, city, area]
				});
				wx.navigateBack({
					delta: 1
				});
			} else {
				wx.navigateBack({
					delta: 1
				});
			}
			// wx.navigateTo({
			// 	url: '../list/list?province=' + province + "&city=" +
			// 		city + "&area=" + area,
			// });
			// wx.navigateBack({
			// 	delta: 1
			// });
		}).catch(function(ex) {
			console.log(ex);
			wx.showToast({
				title: '添加失败',
				icon: 'none',
				duration: 2000

			});
		});
	},
	onChange: function(e) {
		this.setData({
			streetName: e.detail
		})

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		userinfo = wx.getStorageSync('useridentity');
		if (options.province && options.city && options.area) {
			this.setData({
				region: [options.province, options.city, options.area],
				userinfo: userinfo
			})
		}

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		debugger;
	
			let pages = getCurrentPages();
			let prevPage = pages[pages.length - 2]; //上一个页面

			let province = this.data.region[0];
			let city = this.data.region[1];
			let area = this.data.region[2];
			if(prevPage)
			{
				//debugger;
				//直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
				prevPage.setData({
					region: [province, city, area]
				});
			}
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
